package com.jbit.springboot_redis.controller;


import com.jbit.springboot_redis.entity.User;
import com.jbit.springboot_redis.redis.RedisUtil;
import com.jbit.springboot_redis.service.IUserService;
import com.jbit.springboot_redis.service.impl.UserServiceImpl;
import com.jbit.springboot_redis.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-06-08
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
  private Logger logger= LoggerFactory.getLogger(UserController.class);
  @Autowired
  private UserServiceImpl userService;
  @Autowired
  private RedisUtil redisUtil;
//  @CrossOrigin
  @PostMapping("/login")
  private Result login(@RequestBody User user){
    System.out.println("方法进来了");
    System.out.println(user.getUsername()+":"+user.getPassword());
    User returnUser=userService.getUserByNameAndPwd(user.getUsername(),user.getPassword());
    Result<User> result=new Result<>();
    if(returnUser !=null){
      result.setData(returnUser);
      result.setCode(200);
      result.setMessage("登录成功");
      redisUtil.set(returnUser.getUsername(),user);
      logger.info("登录成功,数据存入redis"+returnUser.getUsername());
    }else{
      result.setCode(500);
      result.setMessage("登录失败");
    }
    return result;
  }



}
